﻿using System;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Text;

namespace yunio._req_http
{
    public class NormalWeb
    {
        internal const string METHOD_GET = "GET";
        internal const string METHOD_POST = "POST";
        internal const string METHOD_PUT = "PUT";
        internal const string JSON = "application/json";
        internal const string FormData = "application/x-www-form-urlencoded";
        public static HttpWebRequest NewRequest(string method, string uri, string contentType = null)
        {
            var request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = method;
            request.Timeout = 30000; // 30秒超时
            request.UserAgent = YunioHttp.UserAgent;
            request.KeepAlive = false;
            request.ContentLength = 0;
            if (contentType != null) request.ContentType = contentType;
            return request;
        }
        public static HttpWebRequest Get(string uri)
        {
            return NewRequest(METHOD_GET, uri);
        }
        public static HttpWebRequest Post(string uri)
        {
            return NewRequest(METHOD_POST, uri);
        }
        public static HttpWebRequest PostJson(string uri)
        {
            return NewRequest(METHOD_POST, uri, JSON);
        }
        public static HttpWebRequest PostFormData(string uri)
        {
            return NewRequest(METHOD_POST, uri, FormData);
        }
        public static HttpWebRequest Put(string uri)
        {
            return NewRequest(METHOD_PUT, uri);
        }

        public static HttpWebResponse GetResponse(HttpWebRequest request)
        {
            // 获取响应
            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex) when (ex.Response is HttpWebResponse resp)
            {
                response = resp;
            }
            return response;

        }
        public static Stream GetStream(HttpWebResponse response)
        {
            if (response != null)
            {
                var stream = response.GetResponseStream();
                if (stream != null)
                {
                    if (response.ContentEncoding == "gzip")
                        return new GZipStream(response.GetResponseStream(), CompressionMode.Decompress);
                    else if (response.ContentEncoding == "deflate")
                        return new DeflateStream(response.GetResponseStream(), CompressionMode.Decompress);
                    return response.GetResponseStream();
                }
            }
            return Stream.Null;
        }
        public static StreamReader OpenReader(HttpWebResponse response)
        {
            if (response != null)
            {
                Encoding encoding;
                {
                    var charset = response.CharacterSet;
                    if (!string.IsNullOrEmpty(charset))
                        encoding = Encoding.GetEncoding(charset);
                    else
                        encoding = Encoding.UTF8;
                }
                return new StreamReader(GetStream(response), encoding);
            }
            return new StreamReader(Stream.Null);
        }
        public static void WriteJson(HttpWebRequest request, YunioJson json)
        {
            byte[] byteArray = Encoding.UTF8.GetBytes(json.ToJson());
            request.ContentLength = byteArray.Length;

            using (var requestStream = request.GetRequestStream())
                requestStream.Write(byteArray, 0, byteArray.Length);
        }
        public static void WriteFormData(HttpWebRequest request, string formdata)
        {
            byte[] byteArray = Encoding.UTF8.GetBytes(formdata);
            request.ContentLength = byteArray.Length;

            using (var requestStream = request.GetRequestStream())
                requestStream.Write(byteArray, 0, byteArray.Length);
        }
        public static void WriteMultiContent(HttpWebRequest request, MultiContent multiContent)
        {
            multiContent.OnBuildingHeader(request);
            using (Stream requestStream = request.GetRequestStream()) multiContent.WriteContent(requestStream);
        }
    }
}
